Changing the contents of a Text Object

You can use text objects in a variety of different ways:

Text objects are very flexible and powerful reporting tools.

To change the contents of a text object you use the SetText method.

Note:    The following examples change the contents of a Text object during the Load event of the Form containing the Crystal Report Viewer ActiveX control. This is especially important if the value of your text object is dependent on data in your data source. Since this is also a common place to change the data source used by a report, changing values in Text objects during the Load event often becomes convenient.

Making a simple text change

This example simply assigns a string to the Text1 object in the report.

Private Sub Form_Load()
    Dim CRXReport As New CrystalReport1
    ' More code here
    CRXReport.Text1.SetText "Here is some text from my app"
    CRViewer1.ReportSource = CRXReport
    CRViewer1.ViewReport
End Sub

Displaying a calculated result

A more complicated technique may be to access data in a database, calculate a new value in Visual Basic, and then display that value through a Text object on the report. In such cases, you can supply a text object for each result when you design your report in RDC Designer. The following code assumes that you have done this:

Private Sub Form_Load()

Set up the variables.

    Dim maxValue As Currency
    Dim maxValueString As String
    Dim CRXReport As New CrystalReport1
    Dim rs As New ADODB.Recordset

Set your data source.

rs.Open "SELECT * FROM Customer", _
"DSN=Xtreme Sample Database;", adOpenKeyset CRXReport.Database.SetDataSource rs maxValue = 0

Read the records and calculate the results.

    rs.MoveFirst
    Do While Not rs.EOF
        If rs.Fields("Last Year's Sales") > maxValue Then
            maxValue = rs.Fields("Last Year's Sales")
        End If
        rs.MoveNext
    Loop

Format and assign the results to the text object.

    maxValueString = "The maximum value is " & _
        Format(maxValue, "Currency")     CRXReport.Text1.SetText maxValueString

Preview the report.

    CRViewer1.ReportSource = CRXReport
    CRViewer1.ViewReport End Sub

In this example, we are finding the maximum value of the Last Year's Sales field from the new data source, formatting that value as Currency, then displaying a message containing the value through a Text object on the report. As you can see, Text objects provide many options for controlling the output of data in your reports.



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com